﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace projetoFinal
{
    public class operaTipoServico
    {
        public static List<tipoServico> listar(string field, string value, int crit)
        {
            List<tipoServico> listTipoServico = new List<tipoServico>();
            string strSQL = string.Empty;

            if (field.Equals(""))
            {
                strSQL = " SELECT * FROM tiposervico ";
            }
            else
            {
                strSQL = DAOHelper.sqlQueryBuilder("tiposervico", field, value, crit);
            }

            using (IDataReader dr = DAOHelper.executeQuery(strSQL))
            {
                while (dr.Read())
                {
                    tipoServico objTipoServico = new tipoServico(Int64.Parse(dr["codTipoServico"].ToString()), dr["descr"].ToString());
                    listTipoServico.Add(objTipoServico);
                }
            }
            return listTipoServico;
        }

        public static int incluir(tipoServico objTipoServico)
        {
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append(" INSERT INTO ");
            strSQL.Append("      tiposervico ");
            strSQL.Append(" ( ");
            strSQL.Append("    descr ");
            strSQL.Append(" ) ");
            strSQL.Append(" VALUES ");
            strSQL.Append(" ( ");
            strSQL.AppendFormat(" {0} ", "'" + objTipoServico.Descr + "'");
            strSQL.Append("  ); ");
            return DAOHelper.executeDML(strSQL.ToString());
        }

        public static int apagar(Int64 codTipoServico)
        {
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append(" DELETE FROM tiposervico ");
            strSQL.Append(" WHERE ");
            strSQL.Append(" codTipoServico = ");
            strSQL.AppendFormat("{0}; ", codTipoServico);
            return DAOHelper.executeDML(strSQL.ToString());
        }

        public static int editar(tipoServico objTipoServico)
        {
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append(" UPDATE tiposervico ");
            strSQL.Append(" SET ");
            strSQL.AppendFormat(" descr = {0} ", "'" + objTipoServico.Descr + "'");
            strSQL.Append(" WHERE ");
            strSQL.AppendFormat(" codTipoServico = {0}; ", objTipoServico.CodTipoServico);
            return DAOHelper.executeDML(strSQL.ToString());
        }

        public static tipoServico buscar(Int64 codTipoServico)
        {
            tipoServico objTipoServico;
            List<tipoServico> listaTipoServico = listar(" codTipoServico ", codTipoServico.ToString(), 1);
            if (listaTipoServico.Count == 1)
            {
                objTipoServico = listaTipoServico[0];
            }
            else
            {
                objTipoServico = null;
            }
            return objTipoServico;
        }
    }
}
